{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: tinydb in c:\\users\\jn-sh\\appdata\\local\\programs\\python\\python311\\lib\\site-packages (4.8.0)\n"
     ]
    }
   ],
   "source": [
    "# 安装tinydb数据库软件包并导入当前代码\n",
    "%pip install tinydb\n",
    "from tinydb import TinyDB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三,同事,13511112222\n",
      "\n",
      "李四,朋友,13900001111\n",
      "\n",
      "王武,同事,18912344321\n"
     ]
    }
   ],
   "source": [
    "# 指定存放通讯录的文件\n",
    "db = TinyDB('./db.json')\n",
    "\n",
    "with open(\"./demo.csv\",\"r\",encoding='utf-8') as f:\n",
    "    # 迭代每行数据\n",
    "    for line in f:\n",
    "        print(line)\n",
    "        line_data = line.split(',')\n",
    "        friend = {'name':line_data[0], 'source':line_data[1], 'tel':line_data[2].strip()}\n",
    "        # 将通讯录中的好友信息写入数据库\n",
    "        db.insert_multiple([\n",
    "            friend\n",
    "        ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'name': '张三', 'source': '同事', 'tel': '13511112222'},\n",
       " {'name': '李四', 'source': '朋友', 'tel': '13900001111'},\n",
       " {'name': '王武', 'source': '同事', 'tel': '18912344321'}]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看通讯录中全部好友\n",
    "db.all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'name': '张三', 'source': '同事', 'tel': '13511112222'}, {'name': '张三', 'source': '同事', 'tel': '13511112222'}, {'name': '张三', 'source': '同事', 'tel': '13511112222'}]\n",
      " 张三 的 电话是 13511112222 \n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\nTinyDB 是纯Python实现的数据库\\n它还能支持更新、删除、复杂查询等操作\\n更多功能请参考官方文档\\nhttps://pypi.org/project/tinydb/\\n'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 根据姓名查电话\n",
    "from tinydb import Query\n",
    "friend = Query()\n",
    "friend_info = db.search(friend.name == \"张三\")\n",
    "\n",
    "print(friend_info)\n",
    "\n",
    "print(f\" {friend_info[0]['name']} 的 电话是 {friend_info[0]['tel']} \" )\n",
    "\n",
    "\n",
    "\"\"\"\n",
    "TinyDB 是纯Python实现的数据库\n",
    "它还能支持更新、删除、复杂查询等操作\n",
    "更多功能请参考官方文档\n",
    "https://pypi.org/project/tinydb/\n",
    "\"\"\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "vscode": {
   "interpreter": {
    "hash": "397704579725e15f5c7cb49fe5f0341eb7531c82d19f2c29d197e8b64ab5776b"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
